<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
namespace core;
defined('BY_JYA') or exit('error');
class ssr{
	public function sitemap() {
		$params=array();	
		$links=array();
		$txturl='';
		$xmlurl='';
		$list=pdo_getall('core_account_group',$params,array('id','title'),'','id desc');
		$links['group']=array(
				'title'=>'服务套餐/场景套餐',
				'url'=>manage_url('core/home.group_store/list/',true)
		);
		$txturl .=$links['group']['url']."\n";
		$xmlurl .="<url><loc>".$links['group']['url']."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
		foreach($list as $item){		
			$links['group']['apps'][]=array(
				'title'=>$item['title'],
				'url'=>manage_url('core/home.group_store/detail/id='.$item['id'],true)
			);
			$url=manage_url('core/home.group_store/detail/id='.$item['id'],true);
			$txturl .=$url."\n";
			$xmlurl .="<url><loc>".$url."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
		}
		$list=pdo_getall('core_article_group',$params,array('id','title'),'','id desc');
		$links['news']=array(
				'title'=>'新闻动态',
				'url'=>manage_url('core/home.news/list/',true)
		);
		$url=manage_url('core/home.news/list/',true);
		$txturl .=$url."\n";
		$xmlurl .="<url><loc>".$url."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
		foreach($list as $item){		
			$links['news']['apps'][]=array(
				'title'=>$item['title'],
				'url'=>manage_url('core/home.news/detail/id='.$item['id'],true)
			);
			$url=manage_url('core/home.news/detail/id='.$item['id'],true);
			$txturl .=$url."\n";
			$xmlurl .="<url><loc>".$url."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
		}
		$uniacids=pdo_getall('core_account_plugin_setting',array('uniacid >'=>0,'plugin'=>'jy_weishop','key'=>'shopset'),array('uniacid','data'));
		foreach($uniacids as $item){
			$url=custom_url('jy_weishop/index/',true,$item['uniacid']);
			$txturl .=$url."\n";
			$xmlurl .="<url><loc>".$url."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
			$params=array('uniacid'=>$item['uniacid'],'merchid'=>0,'mid'=>0,'parentid'=>0);
			$fields=array('id','uniacid','title','paixu');
			$list=pdo_getall('jy_weishop_goods_group',$params,$fields);
			foreach($list as $item){		
				$url=custom_url('jy_weishop/goods.index//pid='.$item['id'],true,$item['uniacid']);
				$txturl .=$url."\n";
				$xmlurl .="<url><loc>".$url."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
				$params=array('uniacid'=>$item['uniacid'],'pcate'=>$item['id'],'status'=>1,'type <>'=>4,'nochecked'=>0,'no_showsys'=>0,'merchid'=>0);
				$fields=array('id','uniacid','title','status','paixu');
				$list=pdo_getall('jy_weishop_goods',$params,$fields);
				foreach($list as $gv){
						$url=custom_url('jy_weishop/goods.detail//id='.$gv['id'],true,$item['uniacid']);
						$txturl .=$url."\n";
						$xmlurl .="<url><loc>".$url."</loc><lastmod></lastmod><changefreq></changefreq><priority>0.8</priority></url>\n";
				}
			}
		}
		if(!file_exists(ROOT_AT.'sitemap')){
			mkdir(ROOT_AT.'sitemap',0700, true);
		}
		file_put_contents(ROOT_AT.'sitemap/sitemap.txt',$txturl);
		$xmlurl=str_replace('&','&amp;',$xmlurl);
		$sitemapxml=$this->xml($xmlurl);
		file_put_contents(ROOT_AT.'sitemap/sitemap.xml',$sitemapxml);
	}
	public function xml($xmlurl) {
		$xml=<<<TPL
<?xml version="1.0" encoding="utf-8"?>
<urlset>
{$xmlurl}
</urlset>
TPL;
		return ltrim($xml);
	}
	public function upurl($url='',$ismanage=0){
		$ssr=core_setting('ssr');
		if(!$ssr['token']){
			return '';
		}
		$site=SITEROOT;
		if(substr($site,-1)=='/'){
			$site=substr($site,0,-1);
		}
		$rurl='http://data.zz.baidu.com/urls?site='.$site.'&token='.$ssr['token'];
		$url=$ismanage?manage_url($url,true):custom_url($url,true);
		$post[]=$url;
		$result=cfc('http')->ihttp_post($rurl,$post);
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=@json_decode($data,true);
		//print_r($data);exit;
		return $data;
	}
}